package com.wu.rabbitmq.three;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.wu.rabbitmq.utils.RabbitMQUtils;
import com.wu.rabbitmq.utils.SleepUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @program: RabbitMQ-Study
 * @author: 吴磊
 * @create: 2021-12-14 11:18
 */
public class Work04 {
    private static final String TASK_ACK_QUEUE = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("C2等待消息处理，时间较长");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            //睡眠10s
            SleepUtils.sleep(10);
            System.out.println("接收到的消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
            //手动应答
            /**
             * 1.消息的标记  tag （每一个消息都有一个唯一的标识）
             * 2.是否批量应答  false  不批量应答信道中的消息   true  表示批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        CancelCallback cancelCallback = consumerTag -> {
            System.out.println(consumerTag + "消息被取消掉的回调逻辑");
        };

        //不公平分发
        channel.basicQos(5);

        //采用手动应答
        channel.basicConsume(TASK_ACK_QUEUE, false, deliverCallback, cancelCallback);
    }
}
